package com.wnn.demo.model.news.service.impl;//package org.springblade.system.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wnn.demo.domain.Accessory;
import com.wnn.demo.domain.News;
import com.wnn.demo.domain.Newsque;
import com.wnn.demo.domain.Task;
import com.wnn.demo.model.news.mapper.NewsMapper;
import com.wnn.demo.model.news.service.NewsService;
import com.wnn.demo.model.task.mapper.AccessoryFileMapper;
import com.wnn.demo.model.task.mapper.TaskMapper;
import com.wnn.demo.model.task.service.TaskService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@AllArgsConstructor
public class NewsServiceImpl implements NewsService {

	private NewsMapper newsMapper;

	private AccessoryFileMapper accessoryFileMapper;

	@Override
	public void save(News news) { newsMapper.save(news); }

	@Override
	public Integer submit(String id) {
		 return newsMapper.submit(id);
	}

	@Override
	public Integer back(String id) {
		return newsMapper.back(id);
	}


//	@Override
//	public List<Task> findList() {
//		return taskMapper.findList();
//	}

	@Override
	public News selectOne(News news) {
		return newsMapper.selectOne(news);
	}


	@Override
	public Integer deletedOne(String id) {
		Integer one = newsMapper.deletedOne(id);
		return one;
	}

	@Override
	public void update(News news) {
		newsMapper.update(news);
	}

	@Override
	public Integer submitId(String id) {
		return newsMapper.submitId(id);
	}


	@Override
	public PageInfo<News> findAuditList(String titles, String createBy, String startTime, String endTime, Integer page, Integer limit) {
		PageHelper.startPage(page,limit);
		List<News> info= newsMapper.findAuditList(titles, createBy, startTime, endTime);
		if(page>=2 && info.size()==0){
			page = 1;
			PageHelper.startPage(page,limit);
			info = newsMapper.findAuditList(titles, createBy, startTime, endTime);
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}else{
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}
		return new PageInfo<News>(info);
	}

	@Override
	public PageInfo<News> selectNewByName(Newsque newsque,Integer page, Integer limit) {
		PageHelper.startPage(page,limit);
		List<News> info= newsMapper.selectNewByName(newsque);
		for (int i = 0; i < info.size(); i++) {
			System.out.println(info.get(i).getId());
			List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
			info.get(i).setAccessories(file);
		}
		return new PageInfo< >(info);
	}


	@Override
	public PageInfo<News> findReleaseNews(String titles, String createBy, String startTime, String endTime,Integer page, Integer limit) {
		PageHelper.startPage(page,limit);
		List<News> info= newsMapper.findReleaseNews(titles, createBy, startTime, endTime);
		if(page>=2 && info.size()==0){
			page = 1;
			PageHelper.startPage(page,limit);
			info = newsMapper.findReleaseNews(titles, createBy, startTime, endTime);
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}else{
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}
		return new PageInfo<News>(info);
	}

	@Override
	public List<News> selectByTime(News news) {
		return newsMapper.selectByTime(news);
	}



	@Override
	public PageInfo<News> selectNewsAll(String titles, String createBy, String startTime, String endTime, Integer page, Integer limit) {
		PageHelper.startPage(page,limit);
		List<News> info= newsMapper.findList(titles, createBy, startTime, endTime);
		if(page>=2 && info.size()==0){
			page = 1;
			PageHelper.startPage(page,limit);
			info = newsMapper.findList(titles, createBy, startTime, endTime);
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}else{
			for (int i = 0; i < info.size(); i++) {
				System.out.println(info.get(i).getId());
				List<Accessory> file = accessoryFileMapper.findFile(info.get(i).getId());
				info.get(i).setAccessories(file);
			}
		}
		return new PageInfo<News>(info);
	}
}
